const config = require('./config/config')
const express = require('express')
const bodyparse = require('body-parser')

const app = express()
// 解决跨域问题
app.all('/*', function (req, res, next) {
  // 跨域处理
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('X-Powered-By', ' 3.2.1')
  res.header('Content-Type', 'application/json;charset=utf-8')
  next() // 执行下一个路由
})

// 客户端调用接口，生成token
// let accessKey = 'QqMmrTCI_dPlVNM0HWFDfuf7PbC0WVa0VlMzOej9';
// let secretKey = '9YuxNu_91por3Unupi7KrVzm6R_AmMuPOY8YXJcU';
// let mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
// let options = {
//   scope: 'yuki-space' //七牛资源目录
// };
// let putPolicy = new qiniu.rs.PutPolicy(options);
// let uploadToken = putPolicy.uploadToken(mac);
// console.log(uploadToken);

// const app = express()
// 解析数据
app.use(bodyparse.json())
// 处理请求
app.get('/token', (req, res, next) => {
  res.status(200).send(config.uploadToken)
})

  // http.createServer(function (req, res) {
  //   res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  //   res.end(util.inspect(config.uploadToken));
  // })
  .listen(config.port, config.domain, () => {
    console.log(`http server start on http://${config.domain}:${config.port}`)
  })
  // 把uploadToken返回给客户端

// .listen(9527, () => {
//   console.log('this server are running on localhost:3000!')
// })
